package function;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import gui.IconPanel;

public class IconGetting {

	/**
	 * Class: IconPanel
	 * Description: getting icon from a directory
	 * @category function
	 * @author Mr3chi
	 * @throws IOException 
	 * @since 04/05/2013
	 *
	 */
	public static final int ONLY_FOLDER = 0;
	public static final int ONLY_FILE = 1;
	public static final int BOLD = 2;
	
	public static final ArrayList<IconPanel> getIconsfromDirectory(String directory, int gettingType) throws IOException{
		ArrayList<IconPanel> array = new ArrayList<IconPanel>();
		File folder = new File(directory);
		if(folder.isDirectory()){
			for (File fileEntry : folder.listFiles()) {
		        if (fileEntry.isDirectory()) {
		            //folder detected
		        	if(gettingType == BOLD || gettingType == ONLY_FOLDER){
		        		array.add(new IconPanel(getIconfromFile(fileEntry), fileEntry.getName(), 32));
		        	}
		        } else {
		            //file detexted
		        	if(gettingType == BOLD || gettingType == ONLY_FILE){
		        		array.add(new IconPanel(getIconfromFile(fileEntry), fileEntry.getName(), 32));
		        	}
		        }
		    }
		}
		return array;
	}
	
	public static ImageIcon getIconfromFile(File file) throws IOException {
		 sun.awt.shell.ShellFolder sf =
		            sun.awt.shell.ShellFolder.getShellFolder(file);
		 ImageIcon icon = new ImageIcon(sf.getIcon(true));
		 return icon;
	}
}
